package com.zking.frame;

import com.zking.dao.FoodDao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class Upate extends JDialog {
    //商铺分类，商铺名称，菜品名，菜品价格，菜品图片地址，商家图片地址，商家展示。
    private JLabel sortJLabel,shopJLabel,nameJLabel,priceJLabel,ptadd_JLabel,stadd_JLabel,showJLabel;
    private JTextField sortComboBox;// 商铺分类下拉框
    //商铺名称，菜品名，菜品价格，菜品图片地址，商家图片地址，商家展示。
    private JTextField shopField,nameField,priceField,ptadd_Field,stadd_Field,showField;;// 商铺分类下拉框
    private JButton jButton;//确定修改
    private JPanel jPanel;
    private FoodDao foodDao = new FoodDao();
    private Client client;
    private boolean ab = false, bc = false, cd = false;
    public Upate(JFrame owner, boolean modal,String title,String names, String name,String txt_name){
        super(owner,modal);
        this.client = (Client) owner;
        // 设置名字
        setTitle(title);
        // 设置背景颜色
        getContentPane().setBackground(Color.white);
        // 设置窗口大小
        setSize(700, 800);
        // 取消布局
        setLayout(null);
        // 设置居中
        setLocationRelativeTo(null);

        jPanel = new JPanel();
        jPanel.setBounds(0,0,686,700);
        jPanel.setBackground(Color.white);
        jPanel.setLayout(null);
        add(jPanel);
        sortJLabel = new JLabel("商铺分类:",JLabel.RIGHT);
        sortJLabel.setBounds(0,25,250,50);
        sortJLabel.setFont(new Font("黑体",Font.BOLD,25));
        jPanel.add(sortJLabel);
        shopJLabel = new JLabel("商铺名称:",JLabel.RIGHT);
        shopJLabel.setBounds(0,125,250,50);
        shopJLabel.setFont(new Font("黑体",Font.BOLD,25));
        jPanel.add(shopJLabel);
        nameJLabel = new JLabel("菜品名:",JLabel.RIGHT);
        nameJLabel.setBounds(0,225,250,50);
        nameJLabel.setFont(new Font("黑体",Font.BOLD,25));
        jPanel.add(nameJLabel);
        priceJLabel = new JLabel("菜品价格:",JLabel.RIGHT);
        priceJLabel.setBounds(0,325,250,50);
        priceJLabel.setFont(new Font("黑体",Font.BOLD,25));
        jPanel.add(priceJLabel);
        ptadd_JLabel = new JLabel("菜品图片地址:",JLabel.RIGHT);
        ptadd_JLabel.setBounds(0,425,250,50);
        ptadd_JLabel.setFont(new Font("黑体",Font.BOLD,20));
        jPanel.add(ptadd_JLabel);
        stadd_JLabel = new JLabel("商家图片地址:",JLabel.RIGHT);
        stadd_JLabel.setBounds(0,525,250,50);
        stadd_JLabel.setFont(new Font("黑体",Font.BOLD,20));
        jPanel.add(stadd_JLabel);
        showJLabel = new JLabel("商家展示图片地址:",JLabel.RIGHT);
        showJLabel.setBounds(0,625,250,50);
        showJLabel.setFont(new Font("黑体",Font.BOLD,20));
        jPanel.add(showJLabel);

        sortComboBox = new JTextField(names);
        sortComboBox.setBounds(260,25,350,50);
        sortComboBox.setEditable(false);//设置不能更改
        sortComboBox.setFont(new Font("黑体",Font.BOLD,25));
        sortComboBox.setBackground(Color.white);
        jPanel.add(sortComboBox);

        shopField = new JTextField(name);
        shopField.setBounds(260,125,350,50);
        shopField.setEnabled(false);//设置不能更改
        shopField.setFont(new Font("黑体",Font.BOLD,25));
        jPanel.add(shopField);
        nameField = new JTextField();
        nameField.setBounds(260,225,350,50);
        nameField.setFont(new Font("黑体",Font.BOLD,25));
        jPanel.add(nameField);
        priceField = new JTextField();
        priceField.setBounds(260,325,350,50);
        priceField.setFont(new Font("黑体",Font.BOLD,25));
        jPanel.add(priceField);
        ptadd_Field = new JTextField();
        ptadd_Field.setBounds(260,425,350,50);
        ptadd_Field.setFont(new Font("黑体",Font.BOLD,25));
        jPanel.add(ptadd_Field);
        stadd_Field = new JTextField();
        stadd_Field.setBounds(260,525,350,50);
        stadd_Field.setFont(new Font("黑体",Font.BOLD,25));
        jPanel.add(stadd_Field);
        showField = new JTextField();
        showField.setBounds(260,625,350,50);
        showField.setFont(new Font("黑体",Font.BOLD,25));
        jPanel.add(showField);

        jButton = new JButton("确认修改");
        jButton.setBounds(260,705,150,50);
        jButton.setFont(new Font("黑体",Font.BOLD,25));
        //添加监听事情,添加商店内容
        add(jButton);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if(  nameField.getText()!=null && priceField.getText()!=null && ptadd_Field.getText()!=null && stadd_Field.getText()!=null
                        && showField.getText()!=null  && !priceField.getText().equals("")) {
                    String shop = shopField.getText();
                    String name = nameField.getText();
                    String priceStr = priceField.getText();
                    double price = Double.parseDouble(priceStr);
                    String ptadd = ptadd_Field.getText();
                    String stadd = stadd_Field.getText();
                    String show = showField.getText();
                    String sort = sortComboBox.getText();
                    foodDao.UpdateFood(name, price, shop, sort, ptadd, stadd, show,txt_name);
                   client.setFoods(client.See(sort, shop));
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(Upate.this, "内容不能为空", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        //设置聚焦失焦事件
        //菜品名字
        nameField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent focusEvent) {
                // TODO Auto-generated method stub
                if (nameField.getText().equals("请输入你的菜品名") || nameField.getText().equals("（只能输入中文、英文）")) {
                    nameField.setText("");
                    nameField.setForeground(Color.gray);
                }

            }
            @Override
            public void focusLost(FocusEvent focusEvent) {
                if (nameField.getText().equals("")) {
                    nameField.setText("请输入你的菜品名");
                    nameField.setForeground(Color.gray);
                    ab = false;
                } else {
                    if (!nameField.getText().matches("^[A-Za-z\\u4e00-\\u9fa5]+$")) {
                        nameField.setText("（只能输入中文、英文）");
                        return;
                    } else {
                        ab = true;
                    }
                }
            }
        });

        priceField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent focusEvent) {
                // TODO Auto-generated method stub
                if (priceField.getText().equals("请输入菜品价格") || priceField.getText().equals("整数或者小数")) {
                    priceField.setText("");
                    priceField.setForeground(Color.gray);
                }

            }
            @Override
            public void focusLost(FocusEvent focusEvent) {
                if (priceField.getText().equals("")) {
                    priceField.setText("请输入菜品价格");
                    priceField.setForeground(Color.gray);
                    bc = false;
                } else {
                    if (!priceField.getText().matches("^[0-9]+\\.{0,1}[0-9]{0,2}")) {
                        priceField.setText("整数或者小数");
                        return;
                    } else {
                        bc = true;
                    }
                }
            }
        });
        // 设置窗口不可调节
        setResizable(false);
        // 设置窗口是否可见
        setVisible(true);
        // 设置窗口关闭模式
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
}
